class ComputersController < ApplicationController
  # GET /computers
  # GET /computers.xml
  
  before_filter :login_required
  
  def index
    @title = "Index Computers"
    @computers = Computer.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @computers }
    end
  end

  # GET /computers/1
  # GET /computers/1.xml
  def show
    @title = "Show Computers"
    @computer = Computer.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @computer }
    end
  end

  # GET /computers/new
  # GET /computers/new.xml
  def new
    @title = "New Computers"
    @computer = Computer.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @computer }
    end
  end

  # GET /computers/1/edit
  def edit
    @title = "Edit Computers"
    @computer = Computer.find(params[:id])
  end

  # Struct for graph
  ComputerByStartDate = Struct.new(:status, :count)
  ComputerCountByOs = Struct.new(:os, :count)

  # POST /computers
  # POST /computers.xml
  def create
    @title = "Create Computers"
    @computer = Computer.new(params[:computer])
    if (@computer.room != nil && @computer.room.capacity > @computer.room.computers.size)
      if request.xhr?
        if @computer.save
          @companies = Company.all
          @computers = []
          for computer in Computer.all
            if (computer.room_id == nil) 
              @computers << computer
            end
          end
          @computers_counts = Computer.count(:group => "status").collect do |status, count|
            ComputerByStartDate.new(status, count)
          end
          
          @computer_os_counts = Computer.count( :group => "os" ).collect do |os, count|
            ComputerCountByOs.new(os, count)
          end
          render :update do |page|
            page.replace_html 'tabs-1', :partial => 'supervisions/tree', :locals => {:companies => @companies, :computers => @computers}
            page.replace_html 'overview_panel', :partial => 'layouts/lateral_panel'
            page.replace_html 'computer_graph', :partial => 'supervisions/computer_report', :locals => {:computer_counts => @computer_counts}
            page.replace_html 'os_graph', :partial => 'supervisions/os_report', :locals => {:computer_os_counts => @computer_os_counts}
          end
        else
          render :nothing => true
        end
      else
        respond_to do |format|
          if @computer.save
            flash[:notice] = 'Computer was successfully created.'
            format.html { redirect_to(@computer) }
            format.xml  { render :xml => @computer, :status => :created, :location => @computer }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @computer.errors, :status => :unprocessable_entity }
          end
        end
       end
     else
      if request.xhr?
        render :nothing => true
      else
        respond_to do |format|
          flash[:error] = 'Room null or full.'
          format.html { render :action => "new" }
          format.xml  { render :xml => @computer.errors, :status => :unprocessable_entity }
        end
      end
     end
  end

  # PUT /computers/1
  # PUT /computers/1.xml
  def update
    @title = "Update Computers"
    @computer = Computer.find(params[:id])
    if (@computer.room != nil && @computer.room.capacity > @computer.room.computers.size)
      respond_to do |format|
        if @computer.update_attributes(params[:computer])
          flash[:notice] = 'Computer was successfully updated.'
          format.html { redirect_to(@computer) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @computer.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        flash[:error] = 'Room null or full.'
        format.html { render :action => "edit" }
        format.xml  { render :xml => @computer.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /computers/1
  # DELETE /computers/1.xml
  def destroy
    @title = "Destroy Computers"
    @computer = Computer.find(params[:id])
    @computer.destroy

    respond_to do |format|
      format.html { redirect_to(computers_url) }
      format.xml  { head :ok }
    end
  end
  
  def validateIp
    if (!ComputersHelper::pingecho(params[:ip]))
      render :update do |page|
        page.replace_html "computerStatusImg", "<img src='/images/circle_Red.png' />"
        page.replace_html "computer_status", ""
        page.insert_html :bottom, "computer_status" ,content_tag('option', Computer::COMPUTER_STATUS[0], :value => 0 )
        page.visual_effect(:highlight, "computer_ip_address")
      end
    else
      render :update do |page|
        page.replace_html "computerStatusImg", "<img src='/images/circle_Green.png' />"
        page.replace_html "computer_status", ""
        page.insert_html :bottom, "computer_status" ,content_tag('option', Computer::COMPUTER_STATUS[1], :value => 1 )
      end
    end
  end
  
  def add_operation
    
    operation = Operation.find(params[:operation_id])
    computer = Computer.find(params[:computer_id])
    computer.operations << operation
    
    render :update do |page|
      page.remove "operation_#{params[:operation_id]}"
      page.replace_html "computer_status", ""
      page.insert_html :bottom, "computer_status" , content_tag('option', Computer::COMPUTER_STATUS[2], :value => 2 )
      page.insert_html :bottom , 'container_operation', :partial => 'computers/computer_operation', :locals => {:operation => operation}
    end
 
  end

  def delete_operation
    
    operation = Operation.find(params[:operation_id])
    computer = Computer.find(params[:computer_id])
    computer.operations.delete(operation)
    
    render :update do |page|
      page.remove "operation_#{params[:operation_id]}"
      if (computer.operations.size == 0)
        page.replace_html "computer_status", ""
        if ComputersHelper::pingecho(computer.ip_address)
          page.insert_html :bottom, "computer_status" , content_tag('option', Computer::COMPUTER_STATUS[1], :value => 1 )
        else
          page.insert_html :bottom, "computer_status" , content_tag('option', Computer::COMPUTER_STATUS[0], :value => 0 )
        end
      end
      page.insert_html :bottom , 'trash_operation', :partial => 'computers/computer_operation', :locals => {:operation => operation}
    end
    
  end
  
  def add_contact
    
    contact = Contact.find(params[:contact_id])
    computer = Computer.find(params[:computer_id])
    computer.contacts << contact
    
    render :update do |page|
      page.remove "contact_#{params[:contact_id]}"
      page.insert_html :bottom , 'container_contact', :partial => 'computers/computer_contact', :locals => {:contact => contact}
    end
 
  end

  def delete_contact
    
    contact = Contact.find(params[:contact_id])
    computer = Computer.find(params[:computer_id])
    computer.contacts.delete(contact)
      
    render :update do |page|
      page.remove "contact_#{params[:contact_id]}"
      page.insert_html :bottom , 'trash_contact', :partial => 'computers/computer_contact', :locals => {:contact => contact}
    end
    
  end
  
  def get_rooms
    
    if (params[:network_id] != '')
      network = Network.find(params[:network_id])
    
      render :update do |page|
        page.replace_html "computer_room_id", ""
        page.insert_html :bottom, "computer_room_id" ,content_tag('option', '', :value => '' )
        network.rooms.each { |room|
          page.insert_html :bottom, "computer_room_id" ,content_tag('option', "#{room.company.name} : #{room.code}", :value => room.id )
        }
      end
    else
      render :update do |page|
        page.replace_html "computer_room_id", ""
        page.insert_html :bottom, "computer_room_id" ,content_tag('option', '', :value => '' )
        Room.find(:all).each { |room|
          page.insert_html :bottom, "computer_room_id" ,content_tag('option', "#{room.company.name} : #{room.code}", :value => room.id )
        }
      end
    end
  end
  
  def get_networks
    
    if (params[:room_id] != '')
      room = Room.find(params[:room_id])
    
      render :update do |page|
        page.replace_html "computer_network_id", ""
        page.insert_html :bottom, "computer_network_id" ,content_tag('option', '', :value => '' )
        room.networks.each { |network|
          page.insert_html :bottom, "computer_network_id" ,content_tag('option', network.name, :value => network.id )
        }
      end
    else
      render :update do |page|
        page.replace_html "computer_network_id", ""
        page.insert_html :bottom, "computer_network_id" ,content_tag('option', '',:value => '' )
        Network.find(:all).each { |network|
          page.insert_html :bottom, "computer_network_id" ,content_tag('option', network.name, :value => network.id )
        }
      end
    end
    
  end
  
end
